package com.kw.drug.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kw.drug.common.ResultMapUtil;
import com.kw.drug.entity.Druginfo;
import com.kw.drug.entity.Owinfo;
import com.kw.drug.mapper.DruginfoMapper;
import com.kw.drug.service.DruginfoService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 药品相关的controller
 */
@Controller
@RequestMapping(value = "/druginfo")
public class DruginfoController {

    @Autowired
    private DruginfoService druginfoService;

    /**
     * 跳转到保质期页面
     */
    @GetMapping("/warranty")
    public String warranty() {
        return "warranty";
    }

    /**
     * 获取所有药品
     */
    @RequestMapping(value = "/druginfoList")
    @ResponseBody
    public Object druginfoList() {
        List<Druginfo> druginfoList = druginfoService.queryDruginfoList();
        return ResultMapUtil.getHashMapList(druginfoList);
    }


    /**
     * 分页查询药品出入库列表
     */
    @RequestMapping(value = "/druginfoQueryPage")
    @ResponseBody
    public Map owinfoQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<Druginfo> iPage = druginfoService.selectDruginfoPage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 跳到药品信息页面
     */
    @RequestMapping()
    public String druginfoIndex() {
        return "druginfo";
    }

    /**
     * 进入编辑页面
     */
    @RequestMapping("/druginfoQueryByIddruginfoQueryById")

    public String druginfoQueryByIddruginfoQueryById(Integer id, Model model) {
        Druginfo druginfo = druginfoService.selectDrugById(id);
        model.addAttribute("obj", druginfo);
        return "druginfoPage";
    }

    /**
     * 修改药品信息
     */
    @RequestMapping("/druginfoEdit")
    @ResponseBody
    public Map druginfoEdit(Druginfo druginfo) {
        try {
            Integer integer = druginfoService.updateDrugInfo(druginfo);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }

    }

    /**
     * 删除信息
     */

    @RequestMapping("/druginfoDelById")
    @ResponseBody
    public Map druginfoDelById(Integer id) {
        try {
            Integer integer = druginfoService.deleteById(id);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 新增商品页面
     */
    @RequestMapping("/druginfoPage")
    public String addIndex() {
        return "druginfoPage";
    }

    /**
     * 新增商品
     */
    @RequestMapping("/druginfoAdd")
    @ResponseBody
    public Map druginfoAdd(Druginfo druginfo) {
        try {
            Integer integer = druginfoService.addDrugInfo(druginfo);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
}





















